<template>
	<view class="container">
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancel-button="none" placeholder="请输入商品进行搜索"></uni-search-bar>
		</view>
		<view class="suggest-list" v-if="searchResult.length !== 0">
			<view class="sugg-item" v-for="(item, index) in searchResult" :key="index" @click="gotoDetail(item.goods_id)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="deleteHistory"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, index) in historyList" :key="index" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null, // 延时器
				kw: '', // 搜索关键词
				searchResult: [],
				historyList: ['a', 'app', 'apple']
			};
		},
		onLoad() {
			this.historyList = uni.getStorageSync('kw') || []
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchResult()
					
				}, 500)
			},
			async getSearchResult() {
				if (this.kw === '') {
					this.searchResult = []
					
					return
				}
				const {
					data: res
				} = await uni.$http.get(`/api/public/v1/goods/qsearch?query=${this.kw}`)
				if (res.meta.status !== 200) return uni.$showMsg()
				
				this.searchResult = res.message
				this.saveHistory()
				
			},
			gotoDetail(id){
				uni.navigateTo({
					url: `/subpackage/goods_detail/goods_detail?goods_id=${id}`
				})
			},
			saveHistory(){
				let index = this.historyList.findIndex(item => item === this.kw)
				if(index >= 0) this.historyList.splice(index, 1)
				this.historyList.unshift(this.kw)
				uni.setStorageSync('kw', this.historyList)
			},
			
			deleteHistory(){
				this.historyList = []
				uni.setStorageSync('kw', this.historyList)
			},
			gotoGoodsList(item){
				uni.navigateTo({
					url: `/subpackage/goods_list/goods_list?query=${item}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		// background-color: #fff;
		// height: 100vh;
	}
	.search-box {
		background-color: #fb553c;
		// height: 100vh;
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.suggest-list{
		padding: 0 5px;
		.sugg-item {
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px solid #efefef;
			display: flex;
			justify-content: space-between;
			align-items: center;
			.goods-name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}
	
	.history-box {
		padding: 0 5px;
		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #efefef;
			height: 40px;
			font-size: 13px;
		}
		
		.history-list {
			display: flex;
			flex-wrap: wrap;
			.uni-tag {
				margin-top: 5px;
				margin-right: 5px;
			}
		}
	}
</style>